package com.example.player.presentation.adaptor;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.player.R;
import com.example.player.domain.entity.PlayList;

import java.io.File;
import java.util.List;
/**
 * 歌单条目适配器
 * */
public class ListItemAdapter extends RecyclerView.Adapter<ListItemAdapter.ListItemViewHolder> implements RecyclerViewAdapter<List<PlayList>>{
    private List<PlayList> lists;

    public ListItemAdapter(List<PlayList> lists) {
        this.lists = lists;
    }

    @NonNull
    @Override
    public ListItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ListItemViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ListItemViewHolder holder, int position) {
        holder.bind(lists.get(position));
    }
    @Override
    public int getItemCount() {
        return lists.size();
    }
    @Override
    public void setData(List<PlayList> data) {
        this.lists=data;
        notifyDataSetChanged();
    }

    public class ListItemViewHolder extends RecyclerView.ViewHolder{
        //歌单封面
        private ImageView cover;
        //歌单名称
        private TextView name;
        //歌曲数量
        private TextView number;
        //当前视图
        private View view;
        public ListItemViewHolder(@NonNull View itemView) {
            super(itemView);
            this.view=itemView;
            cover=view.findViewById(R.id.iv_cover);
            name=view.findViewById(R.id.tv_playlist_name);
            number=view.findViewById(R.id.tv_song_count);
        }
        public void bind(PlayList playList){
            name.setText(playList.getTitle());
            number.setText(playList.getCount().toString());
            //加载图片
            if(playList.getCoverPath()!=null){
                Glide.with(itemView.getContext())
                        .load(new File(playList.getCoverPath()))
                        .error(R.drawable.song_cover)         // 加载失败时显示的图片
                        .into(cover);
            }else {
                Glide.with(itemView.getContext())
                        .load(R.drawable.song_cover)
                        .into(cover);
            }
        }
    }
}
